package com.sqx.modules.orders.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.orders.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@Api(value = "订单-管理端", tags = {"订单-管理端"})
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;

    @GetMapping("/selectMyOrdersList")
    @ApiOperation("查询我的订单")
    public Result selectMyOrdersList(Integer page, Integer limit, Long userId, Integer status){
        IPage<Orders> ordersIPage = ordersService.page(new Page<>(page, limit), new QueryWrapper<Orders>()
                .eq("user_id", userId).eq(status != null, "status", status));
        return Result.success().put("data",new PageUtils(ordersIPage));
    }

    @GetMapping("/selectOrdersDetails")
    @ApiOperation("查询订单详情")
    public Result selectOrdersDetails(Long ordersId){
        return ordersService.selectOrdersDetails(ordersId,null,null);
    }


    @GetMapping("/selectOrdersList")
    @ApiOperation("查询订单列表")
    public Result selectOrdersList(Integer page,Integer limit,Integer appointmentType,Integer appointmentNum,Integer status,String shipProvince,
                                   String shipCity,String shipDistrict,String shipAddress,String deliveryProvince,String deliveryCity,
                                   String deliveryDistrict,String deliveryAddress,String userName,String phone,String shipUserName,String shipUserPhone,
                                   String driverName,String driverPhone,Integer sort,Long driverRouteId,String longitude,String latitude,
                                   Long driverId,String ordersNo,Integer orderType,Long driverUserId,Long driverRouteIds,Integer isFixedStroke){
        return ordersService.selectOrdersList(page, limit, appointmentType, appointmentNum, status, shipProvince, shipCity, shipDistrict, shipAddress,
                deliveryProvince, deliveryCity, deliveryDistrict, deliveryAddress, userName, phone, shipUserName, shipUserPhone, driverName, driverPhone,
                sort,driverRouteId,longitude,latitude,driverId,ordersNo,orderType,driverUserId,driverRouteIds,isFixedStroke);
    }


    @PostMapping("/takingOrders")
    @ApiOperation("接单")
    public Result takingOrders(Long ordersId,Long userId,Long driverRouteId){
        return ordersService.takingOrders(ordersId, userId,1,driverRouteId);
    }

    @PostMapping("/proceedOrders")
    @ApiOperation("抵达进行中")
    public Result proceedOrders(Long ordersId,String code){
        return ordersService.proceedOrders(ordersId,code);
    }

    @PostMapping("/completeOrders")
    @ApiOperation("完成订单")
    public Result completeOrders(Long ordersId){
        return ordersService.completeOrders(ordersId);
    }

    @ApiOperation("订单分析")
    @GetMapping(value = "/selectOrderByStatus")
    public Result selectOrderByStatus(String date, Integer dateType) {
        return ordersService.selectOrderByStatus(date, dateType);
    }

    @ApiOperation("平台收入统计")
    @GetMapping(value = "/incomeStatistics")
    public Result incomeStatistics(String date, Integer dateType) {
        return ordersService.incomeStatistics(date, dateType);
    }

    @GetMapping("/selectTeamUserList")
    @ApiOperation("获取团队列表")
    public Result selectTeamUserList(Long userId, Integer page, Integer limit, Integer type){
        UserEntity user = userService.selectUserById(userId);
        return ordersService.selectTeamUserList(page,limit,user.getInvitationCode(),type,userId);
    }

    @ApiOperation("平台收入")
    @GetMapping(value = "/platformMoney")
    public Result platformMoney(String dateType, String date) {
        return ordersService.platformMoney(dateType, date);
    }

    @PostMapping("/cancelOrders")
    @ApiOperation("取消订单")
    public Result cancelOrders(Long ordersId){
        return ordersService.cancelOrders(ordersId);
    }


    @PostMapping("/refundOrders")
    @ApiOperation("退款订单")
    public Result refundOrders(Long ordersId){
        return ordersService.refundOrders(ordersId);
    }

    @GetMapping("/selectOrdersCountStatisticsByYear")
    @ApiOperation("订单数量统计")
    public Result selectOrdersCountStatisticsByYear(String startTime,String endTime){
        //总数量 1待支付 2待接单 3已接单 4进行中 5已抵达 6已取消
        List<Integer> ordersCountList=new ArrayList<>();
        List<Integer> ordersDaiFuKuanCountList=new ArrayList<>();
        List<Integer> ordersDaiJieDanCountList=new ArrayList<>();
        List<Integer> ordersYiJieDanCountList=new ArrayList<>();
        List<Integer> ordersJinXingZhongCountList=new ArrayList<>();
        List<Integer> ordersYiDiDaCountList=new ArrayList<>();
        List<Integer> ordersYiQuXiaoCountList=new ArrayList<>();
        List<String> year=new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(parse);
        while (true){
            String dateTime = simpleDateFormat.format(calendar.getTime());
            //状态 1待支付 2待接单 3已接单 4进行中 5已抵达 6已取消
            //总订单数
            Integer ordersCount = ordersService.selectOrdersCountStatisticsByYear(1, dateTime, null);
            ordersCountList.add(ordersCount);
            //1待支付
            Integer ordersDaiFuKuanCount = ordersService.selectOrdersCountStatisticsByYear(1, dateTime, 1);
            ordersDaiFuKuanCountList.add(ordersDaiFuKuanCount);
            //2待接单
            Integer ordersDaiJieDanCount = ordersService.selectOrdersCountStatisticsByYear(1, dateTime, 2);
            ordersDaiJieDanCountList.add(ordersDaiJieDanCount);
            //3已接单
            Integer ordersYiJieDanCount = ordersService.selectOrdersCountStatisticsByYear(1, dateTime, 3);
            ordersYiJieDanCountList.add(ordersYiJieDanCount);
            //4进行中
            Integer ordersJinXingZhongCount = ordersService.selectOrdersCountStatisticsByYear(1, dateTime, 4);
            ordersJinXingZhongCountList.add(ordersJinXingZhongCount);
            //5已抵达
            Integer ordersYiDiDaCount = ordersService.selectOrdersCountStatisticsByYear(1, dateTime, 5);
            ordersYiDiDaCountList.add(ordersYiDiDaCount);
            //6已取消
            Integer ordersYiQuXiaoCount = ordersService.selectOrdersCountStatisticsByYear(1, dateTime, 6);
            ordersYiQuXiaoCountList.add(ordersYiQuXiaoCount);
            year.add(dateTime);
            if(dateTime.equals(endTime)){
                break;
            }
            calendar.add(Calendar.DATE,1);
        }
        Map<String,Object> result=new HashMap<>();
        result.put("ordersCountList",ordersCountList);
        result.put("ordersDaiFuKuanCountList",ordersDaiFuKuanCountList);
        result.put("ordersDaiJieDanCountList",ordersDaiJieDanCountList);
        result.put("ordersYiJieDanCountList",ordersYiJieDanCountList);
        result.put("ordersJinXingZhongCountList",ordersJinXingZhongCountList);
        result.put("ordersYiDiDaCountList",ordersYiDiDaCountList);
        result.put("ordersYiQuXiaoCountList",ordersYiQuXiaoCountList);
        result.put("year",year);
        return Result.success().put("data",result);
    }



}